package cn.xq.pethome.dept.controller;

import cn.xq.pethome.dept.domain.Department;
import cn.xq.pethome.dept.query.DepartmentQuery;
import cn.xq.pethome.dept.service.IDepartmentService;
import cn.xq.pethome.utils.AjaxUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value ="department",description="部门相关的CRUD功能")
public class DepartmentControllor {
    @Autowired
    private IDepartmentService iDepartmentService;

    /*@RequestMapping("/insert")
    @ApiOperation("增加部门")
    public  void insert(Department department){
        iDepartmentService.insert(new Department(63l,
                "篮球","蔡徐坤","/1",
                0,324l,1l,0l));
    }*/
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有部门")
    public List<Department> fingall(){
        return iDepartmentService.fingall();
    }

    @PostMapping("/{id}")
    //swagger中的参数说明
    @ApiOperation("根据id属性查找数据")
    public AjaxUtil findone(@PathVariable("id") Long id){
        try {
            Department fingone = iDepartmentService.fingone(id);
            return AjaxUtil.success().setMsg("查找成功").setData(fingone);
        } catch (Exception e) {
            e.printStackTrace();
        }return AjaxUtil.error().setMsg("查找错误");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value ="删除部门")
    public AjaxUtil delete(@PathVariable("id") Long id){
        try {
            iDepartmentService.delete(id);
            return AjaxUtil.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxUtil.error().setMsg("删除错误");
        }
    }

    @PatchMapping("/batchdel")
    @ApiOperation(value="批量删除")
    public AjaxUtil batchDelete(@RequestBody List<Long> ids){
        try {
            iDepartmentService.batchDelete(ids);
            return AjaxUtil.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxUtil.error().setMsg("批量删除失败");
        }
    }

    //RequestBody是将收到的数据转化为json，在controller方法中使用
    @PostMapping("/pagelist")
    @ApiOperation(value ="/高级查询")
    public AjaxUtil findquery(@RequestBody DepartmentQuery departmentQuery){
        try {
            List<Department> findquery = iDepartmentService.findquery(departmentQuery);
            Long fingquerycount = iDepartmentService.fingquerycount(departmentQuery);
            return AjaxUtil.success().setData(findquery).setTotal(fingquerycount);
        } catch (Exception e) {
            e.printStackTrace();
        }return AjaxUtil.error();

    }

    @PutMapping("/")
    @ApiOperation(value = "保存客户信息【有id则为修改】")
    public AjaxUtil save(@RequestBody Department department){
        try {
            if(department.getId()!=null){
                iDepartmentService.update(department);
                return AjaxUtil.success().setMsg("修改成功");
        }else {
                iDepartmentService.insert(department);
                return AjaxUtil.success().setMsg("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }return AjaxUtil.error();
    }
    @GetMapping("tree")
    @ApiOperation(value = "部门树")
    public List<Department> gettree(){
       return iDepartmentService.departmentTree();
    }
}
